'''
Created on 2017年5月21日

@author: Administrator
'''
import requests

import random
from constant import agent   

   
def validUsefulProxy(proxy):
    
    """
    检验代理可以性   使用代理IP访问百度网站
    :param proxy:
    :return:
    """
    proxies = {"https": "https://{proxy}".format(proxy=proxy)}
    
    ua=random.choice(agent.user_agent_list)   #从user_agent_list中随机抽取出一个字符串z
    header={"User-Agent":ua}
    try:
        # 访问百度网页   https://www.baidu.com/
        r = requests.get('https://www.baidu.com/', proxies=proxies, headers=header ,timeout=5)
        if r.status_code == 200:
            print (proxy,"可用")         
            return True
    except Exception as e:
            print (proxy,"不可用")
            return False

    


def validateIP(proxy): 
    header=random.choice(agent.user_agent_list)
    headers = {'User-Agent': header}    
    session = requests.session()     
    proxys = {'http': proxy,  
             'https': proxy}                  
    url = "http://ip.chinaz.com/getip.aspx"  #用来测试IP是否可用的url  
     
    try:  
        response = session.get(url, proxies=proxys, timeout=5) 
        print (proxy,"可用")            
        return True  
    except Exception as e: 
        print (proxy,"不可用")  
        return False
     
